package org.inspursc.s0517.health.common.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.inspursc.s0517.health.common.converter.RoleConverter2SysRoleDTO;
import org.inspursc.s0517.health.common.dao.SysGroupDao;
import org.inspursc.s0517.health.common.dao.SysGroupRoleDao;
import org.inspursc.s0517.health.common.dao.SysRoleDao;
import org.inspursc.s0517.health.common.dto.SysRoleDTO;
import org.inspursc.s0517.health.common.entity.SysGroup;
import org.inspursc.s0517.health.common.entity.SysGroupRole;
import org.inspursc.s0517.health.common.entity.SysRole;
import org.inspursc.s0517.health.common.service.SysRoleService;
import org.inspursc.s0517.health.common.vo.SysRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.util.List;

@Slf4j
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleDao roleDao;

    @Autowired
    private SysGroupDao groupDao;

    @Autowired
    private SysGroupRoleDao groupRoleDao;

    @Override
    @Transactional
    public SysRoleDTO save(SysRoleDTO sysRoleDTO, List<Integer> groupIds) {
        SysRole sysRole = RoleConverter2SysRoleDTO.converter(sysRoleDTO);
        SysRole role = roleDao.save(sysRole);
        if (null != role) {
            if (!CollectionUtils.isEmpty(groupIds)) {
                if (null != sysRoleDTO.getId()) {
                    groupRoleDao.deleteBySysRoleId(sysRoleDTO.getId());
                }
                groupIds.forEach((groupId) -> {
                    SysGroupRole sysGroupRole = new SysGroupRole();
                    sysGroupRole.setSysGroupId(groupId);
                    sysGroupRole.setSysRoleId(role.getId());
                    groupRoleDao.save(sysGroupRole);
                });
            }
        }
        return sysRoleDTO;
    }

    @Override
    public SysRoleVO findById(Integer id) {
        SysRole role = roleDao.getOne(id);
        List<SysGroup> groupList = groupDao.findByRoleId(id);
        SysRoleVO roleVO = RoleConverter2SysRoleDTO.converterVO(role);
        roleVO.setGroupIds(groupList);
        return roleVO;
    }

    @Override
    public List<SysRole> findAll() {
        return roleDao.findAll();
    }

    @Override
    @Transactional
    public boolean deleteById(Integer id) {
        try {
            roleDao.deleteById(id);
            groupRoleDao.deleteBySysRoleId(id);
            return true;
        } catch (Exception e) {
            log.error("【删除失败】", e.getMessage());
            return false;
        }
    }

    @Override
    public Page<SysRole> findByNameLike(String name, Pageable pageable) {
        return roleDao.findByNameLike(name, pageable);
    }

    @Override
    public List<SysRole> findByUserId(Integer userId) {
        return roleDao.findByUserId(userId);
    }
}
